package com.guess.lolguessing.service.impl;

import com.guess.lolguessing.domain.po.Player;
import com.guess.lolguessing.domain.po.StarComment;
import com.guess.lolguessing.domain.vo.PlayerVO;
import com.guess.lolguessing.mapper.PlayerMapper;
import com.guess.lolguessing.service.IPlayerService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author author
 * @since 2024-10-17
 */
@Service
@RequiredArgsConstructor
public class PlayerServiceImpl extends ServiceImpl<PlayerMapper, Player> implements IPlayerService {

    private final StarCommentServiceImpl starCommentService;
    private final CommentServiceImpl commentService;
    @Override
    public List<PlayerVO> findPlayers(Integer matchId, Integer teamId) {
        List<Player> list = this.lambdaQuery().eq(Player::getTeamId, teamId).list();
        List<PlayerVO> voList = new ArrayList<>();
        for(Player player : list) {
            PlayerVO vo = new PlayerVO();
            vo.setId(player.getPlayerId().longValue());
            vo.setName(player.getPlayerName());
            StarComment starComment = starCommentService.getByMatchIdAndTeamId(matchId, player.getPlayerId());
            vo.setReplies(starComment.getStarNum());
            int number = commentService.findNotZeroNumber(matchId, player.getPlayerId());
            if(number == 0) {
                number = 1;
            }
            vo.setRating(1.0 * starComment.getTotal() / number);
            voList.add(vo);
        }
        return voList;
    }
}
